package servlets;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class EmailSender {
	private final String FROM = "hotronguoidung709@gmail.com";
	private final String PASSW = "kocopass";
	private final String TO = "nluit.tanloc@gmail.com";
	private String subject, content;

	public EmailSender(String subject, String content) {
		this.subject = subject;
		this.content = content;
	}

	public void send() throws AddressException, MessagingException {
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.auth", "true");
		Authenticator aut = new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(FROM, PASSW);
			}
		};
		Session session = Session.getInstance(props, aut);
		Message msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress(FROM));
		msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));
		msg.setSubject(subject);
		msg.setHeader("Content-Type", "text/plain; charset=UTF-8");
		msg.setContent(content, "text/html; charset=UTF-8");
		msg.setSentDate(new Date());

		Transport transport = session.getTransport("smtps");
		transport.connect("smtp.gmail.com", FROM, PASSW);
		Transport.send(msg);
		System.out.println("Sent...");
		transport.close();
	}

	public static void main(String[] args) throws AddressException,
			MessagingException {
		new EmailSender("Cái Bướm", "<html><b>Con Chym</b></html>").send();
	}
}
